﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using RSHB.Framework.BLL;
using System.IO;

namespace RSHB.Framework.ActionResults
{
    public abstract class DownLoadActionResult: ActionResult
    {
        protected abstract string ContentType { get; }

        protected FileContentManager _contentManager;

        public DownLoadActionResult(FileContentManager contentManager)
        {
            this._contentManager = contentManager;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            HttpContext curContext = HttpContext.Current;
            curContext.Response.Clear();
            curContext.Response.AddHeader("content-disposition", "attachment;filename=" + "filename.doc");
            curContext.Response.Charset = "";
            curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            curContext.Response.ContentType = this.ContentType;

            MemoryStream mStream = this._contentManager.GetFileContent();

            byte[] buffer = mStream.ToArray();
            mStream.Flush();
            mStream.Close();
            
            curContext.Response.BinaryWrite(buffer);
            curContext.Response.Flush();            
            curContext.Response.End();
        } 
    }
}
